home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / jazlib.arc / JZINSTR.C < prev    next >
Text File  |  1988-12-18  |  6KB  |  229 lines

  1. /*
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │ Title   : jzinstr                                 │
  4. │ Purpose : Provide a sophisticated means of reading input from the keyboard.│
  5. │        This routine allows editing via the arrow keys and delete, insert│
  6. │        etc are supported as a subset of the wordstar commands.         │
  7. │                                         │
  8. │ Parms:                                     │
  9. │   fstr : string to return input into                         │
  10. │   flen : maximum length of string. Input will be terminated when string is │
  11. │       longer than this value                         │
  12. │   frow : row position of input                         │
  13. │   fcol : starting column position.                         │
  14. │   fattr: color attribute of field. See tech reference manual for details   │
  15. │       or calculate as (Background*16|foreground)&0x7f;             │
  16. │   ftime: Input is timed. If seconds exceed this value, input is terminated │
  17. │       as if the user hit the <ENTER> key.                     │
  18. │   fkeys: string of characters whose ordinal values equal the scan codes of │
  19. │       any special characters which will terminate the input. i.e. If you│
  20. │       wanted the user to be able to quit the read via the ESC key, you  │
  21. │       would pass "\001" which is the scan code for the ESC key. Note    │
  22. │       the routine passes back the keystroke that terminated the read    │
  23. │       and that multiple keys can be put into this field.             │
  24. │   Notes:                                     │
  25. │       The keypad characters are mapped to their wordstar equivalents as │
  26. │       so that UPARROW is mapped to CTRL_E, etc so keep this in mind     │
  27. │       when checking return values.                      │
  28. │                                         │
  29. │    Written by Jack Zucker - 75766,1336    301-794-5950  on          │
  30. └────────────────────────────────────────────────────────────────────────────┘
  31. */
  32.  
  33. jzinstr(fstr,flen,frow,fcol,fattr,ftime,fkeys)
  34. char *fstr;
  35. int flen,frow,fcol,fattr;
  36. long ftime;
  37. char *fkeys;
  38. {
  39.  
  40.  
  41.   static int WINSERT = 0;    /* insert mode flag     */
  42.   int WDONE = 0;        /* init loop condition     */
  43.   int w1,w2,wstart,wend;    /* work variables for cursor */
  44.   char wtemp[256];        /* temporary work string */
  45.   int WNULLFOUND;        /* flag variable     */
  46.   int wcol;            /* work column variable  */
  47.   long wtime,wtics;        /* time accumulator     */
  48.   int wch,wscan;        /* char and scan code     */
  49.  
  50.   wcol    = fcol;         /* work column indicator */
  51.  
  52.   if (MEMB(0x40,0x49) == 3) {    /* color mode */
  53.     wstart = 6;
  54.     wend   = 7;
  55.   }
  56.   else {
  57.     wstart = 12;
  58.     wend   = CTRL_M;
  59.   }
  60.  
  61.   jzsetcur(WINSERT ? wstart-4 : wstart , wend); /* set cursor size */
  62.  
  63.   sprintf(wtemp,"%s%s",fstr,jzpad(flen-strlen(fstr),' '));
  64.   jzscrprn(wtemp,frow,fcol,fattr);
  65.  
  66.   while (! WDONE) {             /* endless loop */
  67.  
  68.     jzloccur(frow,wcol);    /* position cursor correctly */
  69.  
  70.     wtime = time(&wtics);      /* initialize elapsed time counter */
  71.  
  72.     if (wcol - fcol == flen) {
  73.       wch = CTRL_M;
  74.       break;
  75.     }
  76.  
  77.     while ( ! jzkeyprs()) {
  78.       if (time(&wtics) - wtime >= ftime) {
  79.     wch = CTRL_M;
  80.     break;
  81.       }
  82.     }
  83.  
  84.     if (WDONE) break;    /* must have timed out */
  85.  
  86.     /* at this point there is a key sitting in the buffer for us */
  87.  
  88.     wch = jzinkey(&wscan);    /* read char in buffer */
  89.  
  90.     /* look for a command key and exit if hit */
  91.     if (strchr(fkeys,wscan)) {
  92.       wch = wscan;
  93.       WDONE = 1;
  94.     }
  95.     else if (wscan == PLUS) {  /* fake like it's a special key */
  96.       jzredscr(wtemp,frow,fcol,flen);
  97.       strcpy(fstr,jzrgtjst(wtemp,flen));
  98.       jzscrprn(fstr,frow,fcol,fattr);
  99.       WDONE = 1;
  100.       wch = CTRL_M;
  101.     }
  102.  
  103.     if (! WDONE)
  104.       do {
  105.     WNULLFOUND = 0;        /* init boolean        */
  106.     switch (wch) {
  107.       case 0 :           /* some kind of command key */
  108.         WNULLFOUND = 1;       /* got a command key        */
  109.         switch(wscan) {       /* translate arrow keys to wordstar */
  110.           case HOME :
  111.         wch = CTRL_A;
  112.         break;
  113.           case END    :
  114.         wch = CTRL_F;
  115.         break;
  116.           case UARR :
  117.         wch = CTRL_E;
  118.         break;
  119.           case DARR :
  120.         wch = CTRL_X;
  121.         break;
  122.           case LARR :
  123.         wch = CTRL_S;
  124.         break;
  125.           case RARR :
  126.         wch = CTRL_D;
  127.         break;
  128.           case DELETE :
  129.         wch = CTRL_G;
  130.         break;
  131.           case INSERT :
  132.         wch = CTRL_V;
  133.         break;
  134.           case PGUP   :
  135.         wch = CTRL_R;
  136.         break;
  137.           case PGDN   :
  138.         wch = CTRL_C;
  139.         break;
  140.           case BACKTAB :
  141.         wch = BACKTAB;
  142.         break;
  143.  
  144.           default :
  145.         WNULLFOUND = 0;        /* reset to false */
  146.         break;
  147.         }       /* end to inner case statement */
  148.         break;
  149.       case CTRL_E :
  150.       case CTRL_X :
  151.       case CTRL_C :
  152.       case CTRL_R :
  153.         WDONE = 1;
  154.         break;
  155.       case CTRL_G :
  156.         if (flen-(wcol-fcol)-1)
  157.           jzredscr(wtemp,frow,wcol+1,flen-(wcol-fcol)-1);
  158.         else
  159.           wtemp[0] = 0;
  160.         strcat(wtemp," ");
  161.         jzscrprn(wtemp,frow,wcol,fattr);
  162.         break;
  163.       case BACKSPACE :
  164.         if (wcol > fcol) {
  165.           jzredscr(wtemp,frow,wcol,flen-(wcol-fcol));
  166.           strcat(wtemp," ");
  167.           wcol = max(fcol,wcol-1);
  168.           jzscrprn(wtemp,frow,wcol,fattr);
  169.         }
  170.         break;
  171.       case CTRL_A :
  172.         wcol = fcol;      /* back to column 1 of field */
  173.         break;
  174.       case CTRL_F :
  175.         wcol = fcol + flen - 1;   /* end of field */
  176.         break;
  177.       case CTRL_S :
  178.         wcol = max(fcol,wcol-1);
  179.         break;
  180.       case CTRL_D :
  181.         wcol = min(fcol+flen,wcol+1);
  182.         break;
  183.       case CTRL_V :
  184.         WINSERT = ! WINSERT;
  185.         if (WINSERT) jzsetcur(wstart-4,wend);
  186.         else jzsetcur(wstart,wend);
  187.         break;
  188.       case CTRL_M :
  189.         WDONE = 1;
  190.         break;
  191.       case TAB :
  192.         wcol = min(fcol+flen-1,wcol+8);
  193.         break;
  194.       case BACKTAB :
  195.         wcol = max(fcol,wcol-8);
  196.         break;
  197.       default :
  198.         if (wch >= ' ' && wch <= '~')
  199.           if (! WINSERT) {
  200.         jzwrtchr(wch,fattr,1);
  201.         wcol ++;
  202.           }
  203.           else if (flen-(wcol-fcol)-1) {      /* insert mode */
  204.         jzredscr(wtemp,frow,wcol,flen-(wcol-fcol)-1);
  205.         jzwrtchr(wch,fattr,1);
  206.         jzscrprn(wtemp,frow,++wcol,fattr);
  207.           }
  208.           else {
  209.         jzwrtchr(wch,fattr,1);
  210.         ++wcol;
  211.           }
  212.         break;
  213.     }
  214.       } while (WNULLFOUND);
  215.   }
  216.  
  217.   jzsetcur(wstart,wend);     /* restore cursor size */
  218.   jzredscr(fstr,frow,fcol,flen);
  219.  
  220.   -- flen;
  221.   if (fstr[flen] == ' ') {  /* get rid of trailing spaces */
  222.     do
  223.       flen --;
  224.     while (fstr[flen] == ' ');
  225.     fstr[flen + 1] = 0;
  226.   }
  227.   return(wch);             /* maximum length */
  228. }
  229.